﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace KT.Mvvm
{
    public static class PasswordHelper
    {
        public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password",
            typeof(SecureString), typeof(PasswordHelper), new FrameworkPropertyMetadata(default(SecureString), OnPasswordPropertyChanged)
            {
                BindsTwoWayByDefault = true, DefaultUpdateSourceTrigger = UpdateSourceTrigger.LostFocus
            });

        public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach",
            typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach));

        private static readonly DependencyProperty IsUpdatingProperty = DependencyProperty.RegisterAttached("IsUpdating", 
            typeof(bool), typeof(PasswordHelper));

        public static void SetAttach(DependencyObject dp, bool value)
        {
            dp.SetValue(AttachProperty, value);
        }

        public static bool GetAttach(DependencyObject dp)
        {
            return (bool)dp.GetValue(AttachProperty);
        }

        public static SecureString GetPassword(DependencyObject dp)
        {
            return (SecureString)dp.GetValue(PasswordProperty);
        }

        public static void SetPassword(DependencyObject dp, SecureString value)
        {
            dp.SetValue(PasswordProperty, value);
        }

        private static bool GetIsUpdating(DependencyObject dp)
        {
            return (bool)dp.GetValue(IsUpdatingProperty);
        }

        private static void SetIsUpdating(DependencyObject dp, bool value)
        {
            dp.SetValue(IsUpdatingProperty, value);
        }

        private static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var passwordBox = sender as PasswordBox;
            /*passwordBox.PasswordChanged -= PasswordChanged;

            if (!GetIsUpdating(passwordBox))
                passwordBox.SecurePassword = (SecureString)e.NewValue;

            passwordBox.PasswordChanged += PasswordChanged;*/
        }

        private static void Attach(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var passwordBox = sender as PasswordBox;
            if (passwordBox == null) return;

            if ((bool)e.OldValue)
            {
                passwordBox.PasswordChanged -= PasswordChanged;
                passwordBox.LostFocus -= PasswordBoxOnLostFocus;
            }

            if ((bool)e.NewValue)
            {
                passwordBox.PasswordChanged += PasswordChanged;
                passwordBox.LostFocus += PasswordBoxOnLostFocus;
            }
        }

        private static void PasswordBoxOnLostFocus(object sender, RoutedEventArgs routedEventArgs)
        {
            var passwordBox = sender as PasswordBox;
            if (passwordBox == null) return;

            var bindingExpr = passwordBox.GetBindingExpression(PasswordProperty);
            if(bindingExpr.ParentBinding.UpdateSourceTrigger == UpdateSourceTrigger.LostFocus)
                bindingExpr.UpdateSource();
        }

        private static void PasswordChanged(object sender, RoutedEventArgs e)
        {
            var passwordBox = sender as PasswordBox;
            SetIsUpdating(passwordBox, true);
            SetPassword(passwordBox, passwordBox.SecurePassword);
            SetIsUpdating(passwordBox, false);
        }
    }
}
